import java.util.*;

public class Test2 {

    //前k个高频单词
    //https://leetcode.cn/problems/top-k-frequent-words/description/
    public List<String> topKFrequent(String[] words, int k) {
        HashMap<String,Integer> map = new HashMap<>();
        //1. 统计每个单词出现的次数
        for(String word : words) {
            if(map.get(word) == null) {
                map.put(word,1);
            }else {
                int val = map.get(word);
                map.put(word,val+1);
            }
        }

        //2. 建立小根堆
        PriorityQueue<Map.Entry<String,Integer>> minHeap = new PriorityQueue<>(new Comparator<Map.Entry<String, Integer>>() {
            @Override
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                if(o1.getValue().compareTo(o2.getValue()) == 0) {
                    return o2.getKey().compareTo(o1.getKey());
                }
                return o1.getValue().compareTo(o2.getValue());
            }
        });
        //3.遍历map
        for(Map.Entry<String,Integer> entry : map.entrySet()) {
            if(minHeap.size() < k) {
                minHeap.offer(entry);
            }else {
                Map.Entry<String,Integer> top = minHeap.peek();
                if(top.getValue().compareTo(entry.getValue()) < 0) {
                    minHeap.poll();
                    minHeap.offer(entry);
                }else if(top.getValue().compareTo(entry.getValue()) == 0) {
                    if(top.getKey().compareTo(entry.getKey()) > 0) {
                        minHeap.poll();
                        minHeap.offer(entry);
                    }
                }
            }
        }
        ArrayList<String> list = new ArrayList<>();

        for (int i = 0; i < k; i++) {
            Map.Entry<String,Integer> tmp = minHeap.poll();
            list.add(tmp.getKey());
        }
        //e-2 f-3 a-5
        Collections.reverse(list);
        return list;
    }

    //旧键盘
    //https://www.nowcoder.com/questionTerminal/f88dafac00c8431fa363cd85a37c2d5e
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextLine()) { // 注意 while 处理多个 case
            String str1 = in.nextLine();
            String str2 = in.nextLine();
            func(str1,str2);
        }
    }

    public static void func(String str1,String str2) {
        //转成大写
        str1 = str1.toUpperCase();
        str2 = str2.toUpperCase();

        //把坏的键盘的字符串放到 HashSet 中
        HashSet<Character> set1 = new HashSet<>();
        for(int i = 0; i < str2.length();i++) {
            char ch = str2.charAt(i);
            set1.add(ch);
        }

        HashSet<Character> set2 = new HashSet<>();
        //遍历str1 看看set1 和 set2 中是否 存在
        //如果不存在就放到 set2 中 并且 打印
        for(int i = 0; i < str1.length();i++) {
            char ch = str1.charAt(i);
            if (!set1.contains(ch) && !set2.contains(ch)) {
                set2.add(ch);
                System.out.print(ch);
            }
        }
    }

    //宝石与石头
    //https://leetcode.cn/problems/jewels-and-stones/description/
    public int numJewelsInStones(String jewels, String stones) {
        HashSet<Character> set = new HashSet<>();
        for(int i = 0;i < jewels.length();i++) {
            //先把宝石类型的字符串都放到HashSet中
            char ch = jewels.charAt(i);
            set.add(ch);
        }
        int count = 0;
        for(int j = 0;j < stones.length();j++) {
            //根据HashSet中的值和石头的字符一一比较，相同的count++
            char ch = stones.charAt(j);
            if(set.contains(ch)) {
                count++;
            }
        }
        return count;
    }


    //随机链表的复制
    //https://leetcode.cn/problems/copy-list-with-random-pointer/
    class Node {
        int val;
        Node next;
        Node random;

        public Node(int val) {
            this.val = val;
            this.next = null;
            this.random = null;
        }
    }
    public Node copyRandomList(Node head) {
        HashMap<Node,Node> map  = new HashMap<>();
        Node cur = head;

        while (cur != null) {
            Node node = new Node(cur.val);
            map.put(cur,node);
            cur = cur.next;
        }

        cur = head;

        while(cur !=null) {
            map.get(cur).next = map.get(cur.next);
            map.get(cur).random = map.get(cur.random);
            cur = cur.next;
        }

        return  map.get(head);
    }

    //只出现一次的数字
    //https://leetcode.cn/problems/single-number/description/
    public int singleNumber(int[] nums) {
        //方法二：
        HashSet<Integer> set = new HashSet<>();
        for(int s : nums) {
            if(set.contains(s)) {
                set.remove(s);
            }else {
                set.add(s);
            }
        }
        for(int i = 0; i < nums.length;i++) {
            if(set.contains(nums[i])) {
                return nums[i];
            }
        }
        return -1;

        ////方法一：
        // int ret = nums[0];
        // for(int i = 1;i < nums.length; i++) {
        //     ret = ret ^ nums[i];
        // }
        // return ret;
    }

    private static HashMap<String,Integer> countWord(String[] words) {
        HashMap<String,Integer> map = new HashMap<>();
        for(String s : words) {
            if (map.get(s) == null) {
                map.put(s,1);
            }else {
                int value = map.get(s);
                map.put(s,value + 1);
            }
        }
        return map;
    }
    public static void main1(String[] args) {
        //String中，单词出现的次数
        String[] words = {"hello","abc","hello","cede","this","hello","this"};
        HashMap<String,Integer> map = countWord(words);
        //我们使用 k-v 的整体去打印，也就是Set的这个方法
        Set<Map.Entry<String,Integer>> entrySet = map.entrySet();
        for(Map.Entry<String,Integer> entry : entrySet) {
            System.out.println("key: " + entry.getKey() + " value: " + entry.getValue());
        }
    }
}
